package com.zxl.grocery.webmagic.pipeline;

import com.zxl.grocery.webmagic.common.persistence.model.CrawlerNovel;
import com.zxl.grocery.webmagic.service.ICrawlerNovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * info:小说
 * Created by zxl on 2017/8/8 0008.
 */
@Repository
public class NovelPipeline implements Pipeline {
    @Autowired
    private ICrawlerNovelService iCrawlerNovelService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().contains("novel")) {
                CrawlerNovel novel = (CrawlerNovel) entry.getValue();
                HashMap<String, Object> conditionMap = new HashMap<String, Object>();
                conditionMap.put("link", novel.getLink());
                List<CrawlerNovel> novelList = iCrawlerNovelService.selectByMap(conditionMap);

                if (novelList.size() < 1) {//检查链接是否已存在
                    novel.setAuthor("zxl");
                    novel.setSort(0);
                    novel.setStatus(1);
                    novel.setPlacedTop(0);
                    novel.setRecommend(0);
                    novel.setThumbUp(0);
                    novel.setCreateDate(new Date());
                    novel.setModifyDate(new Date());
                    iCrawlerNovelService.insert(novel);
                }
            }
        }
    }
}
